package controllers

import (
	"SimpleDockerPanel/dao"
	"SimpleDockerPanel/pkg/resp"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

type TestController struct{}

func (t TestController) GetUserInfoTest(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		// 处理转换错误
		return
	}
	user, err := dao.GetUserById(id)
	if err != nil {
		resp.Fail(c, 400, err)
	}
	resp.SuccessData(c, 200, "success", user, 1)
}

func (t TestController) SuccessTest(c *gin.Context) {
	resp.SuccessData(c, 200, "success", "this is data", 1)
}

func (t TestController) ErrorTest(c *gin.Context) {
	resp.Fail(c, 400, "bad error")
}

func (t TestController) GetParamsTest(c *gin.Context) {
	id := c.Param("id")
	resp.SuccessData(c, 200, "success", id, 1)
}

// GetNameTest Body
func (t TestController) GetNameTest(c *gin.Context) {
	name := c.PostForm("name")
	age := c.DefaultPostForm("age", "18")
	fmt.Print("name:", name, "age:", age)
	resp.SuccessData(c, 200, age, name, 3)
}

type GetNameJson struct {
	Name string `json:"name"`
	Cid  int    `json:"cid"`
}

// GetNameJsonTest Json
func (t TestController) GetNameJsonTest(c *gin.Context) {
	getNameJson := &GetNameJson{}
	err := c.BindJSON(&getNameJson)
	if err != nil {
		return
	}
	resp.SuccessData(c, 200, getNameJson.Name, getNameJson.Cid, 3)
}
